package com.edu.publisher.controller;

import com.edu.publisher.beans.AppVideoVisit;
import com.edu.publisher.service.AppVideoService;
import com.edu.publisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AppVideoController {


    @Autowired
    private AppVideoService appVideoService;


    @RequestMapping("/appVideo")
    public String getStats(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<AppVideoVisit> appVideoVisitsList = appVideoService.getAppVideo(date);
        if (appVideoVisitsList == null) {
            return "";
        }

        StringBuilder rows = new StringBuilder("[");
        for (int i = 0; i < appVideoVisitsList.size(); i++) {
            AppVideoVisit appVideoVisit = appVideoVisitsList.get(i);
            String chapterName = appVideoVisit.getChapterName();
            String proCot = appVideoVisit.getPeoCount();
            String duringTime = appVideoVisit.getDuringTime();
            Double avgTime = appVideoVisit.getAvPeoTime();
            rows.append("{\n" +
                    "\t\"chapterName\": \"" + chapterName + "\",\n" +
                    "\t\"proCot\": \"" + proCot + "\",\n" +
                    "\t\"duringTime\": \"" + duringTime + "\",\n" +
                    "\t\"avgTime\": " + avgTime + "\n" +
                    "}\n");
            if (i < appVideoVisitsList.size() - 1) {
                rows.append(",");
            } else {
                rows.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"total\": 13,\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"章节名\",\n" +
                "        \"id\": \"chapterName\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"观看人数\",\n" +
                "        \"id\": \"proCot\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"观看时间\",\n" +
                "        \"id\": \"duringTime\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"平均观看时间\",\n" +
                "        \"id\": \"avgTime\"\n" +
                "      }\n" +
                "      \n" +
                "    ],\n" +
                "    \"rows\":"+ rows +"\n" +
                "  }\n" +
                "}\n";
    }
}


